package pos.gengshen.com.gspos.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import pos.gengshen.com.gspos.R;
import pos.gengshen.com.gspos.common.Constant;
import pos.gengshen.com.gspos.pojo.PayRecord;
import pos.gengshen.com.gspos.utils.SpinnerOption;
import pos.gengshen.com.gspos.utils.ZXingUtils;

/**
 * Created by Administrator on 2018/7/16/016.
 */

public class QRCodeActivity extends AppCompatActivity {
    private Button btn_qrcode1;
    private ImageView imageView;
    private Bitmap bitmap;
    private Spinner spinner = null;
    private ArrayAdapter<String> adapter = null;
    private static List<String> carList;




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_qrcode);
        imageView = (ImageView)findViewById(R.id.rqcode);
        btn_qrcode1 = (Button)findViewById(R.id.btn_code1);
        spinner = (Spinner)findViewById(R.id.spinner);
        Intent intent = getIntent();
        final List<SpinnerOption> towns = new ArrayList<SpinnerOption>();
        String carTypeList = intent.getStringExtra("cartypelist");
        JSONArray jsarray = JSON.parseArray(carTypeList);
        carList = new ArrayList<String>();
        for (Iterator iterator = jsarray.iterator(); iterator.hasNext();) {
            JSONObject jsonObject = (JSONObject) iterator.next();
            String cartype = jsonObject .get("carType").toString();
            carList.add(cartype);
            SpinnerOption so = new SpinnerOption(jsonObject.getString("id"),jsonObject.getString("carType"));
            towns.add(so);
        }


        //List<String> list =JSONObject.parseArray(jsarray.toString(),String.class);
        final ArrayAdapter aa=new ArrayAdapter(this,android.R.layout.simple_spinner_item,towns);
        spinner.setAdapter(aa);
        //设置点击事件监听
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

                SpinnerOption so1 = towns.get(i);

                String content = Constant.SERVER_URL+"/index/scanCarTyep?speadUserId=0&code=123456&carTypeId="+so1.getValue();
                if(TextUtils.isEmpty(content)){
                    Toast.makeText(QRCodeActivity.this,"请先输入需要生成二维码的内容",Toast.LENGTH_SHORT).show();
                    return;
                }
                Bitmap bitmap = ZXingUtils.createQRImage(content, imageView.getWidth(), imageView.getHeight());
                imageView.setImageBitmap(bitmap);





            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {



            }
        });
        btn_qrcode1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                QRCodeActivity.this.finish();


            }
        });
    }
}
